﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuntTheWumpus
{

	public struct TriviaStruct
	{
		public string Question;
		public string CorrectAnswer;
		public string FalseAnswer1;
		public string FalseAnswer2;
		public string FalseAnswer3;

		public string[] allAnswers;
	}
	public class Trivia
	{

		List<TriviaStruct> questions = new List<TriviaStruct>();


		public Trivia()
		{
			readDatafile();
		}

		private void readDatafile()
		{
			TextReader tr = new StreamReader("TriviaQuestions.txt");

			string input = tr.ReadLine();

			while (input != null)
			{
				string[] data = input.Split(',');

				TriviaStruct ts;
				ts.Question = data[0];
				ts.CorrectAnswer = data[1];
				ts.FalseAnswer1 = data[2];
				ts.FalseAnswer2 = data[3];
				ts.FalseAnswer3 = data[4];
				ts.allAnswers = new string[4];
				ts.allAnswers[0] = data[1];
				ts.allAnswers[1] = data[2];
				ts.allAnswers[2] = data[3];
				ts.allAnswers[3] = data[4];

				questions.Add(ts);
				//listBoxCities.Items.Add(data[1]);

				input = tr.ReadLine();
			}

			tr.Close();
		}

		public void AddQuestion(TriviaStruct ts)
		{
			questions.Add(ts);
			questions.Sort(delegate(TriviaStruct p1, TriviaStruct p2) { return p1.Question.CompareTo(p2.Question); });

			writeDataToFile();
		}



		private void writeDataToFile()
		{
			TextWriter tw = new StreamWriter("TriviaQuestions.txt");

			foreach (TriviaStruct ts in questions)
			{
				string output = ts.Question + "," +
								ts.CorrectAnswer + "," +
								ts.FalseAnswer1 + "," +
								ts.FalseAnswer2 + "," +
								ts.FalseAnswer3;

				tw.WriteLine(output);
			}

			tw.Close();
		}

		public static int[] shuffle(int[] arr)
		{
			Random rnd = new Random();

			for (int i = 0; i < arr.Length; i++)
			{
				int temp = arr[i];
				int rndVal = rnd.Next(arr.Length);
				arr[i] = arr[rndVal];
				arr[rndVal] = temp;
			}

			return arr;
		}

		//public List<TriviaStruct> GetAllTriviaData()
		//{
		//    return questions;
		//}

		//public TriviaStruct GetAreaCodeData(int index)
		//{
		//    return questions[index];
		//}

		public TriviaStruct getRandomQuestion()
		{
			readDatafile();

			//for (int index = 0; index < questions.Count; index++)


			Random random = new Random();
			int i = random.Next(0, questions.Count - 1);

			return questions[i];
		}

		public string GetQuestion()
		{
			return questions[0].Question;

		}

		public string GetCorrectAnswer()
		{
			return questions[1].CorrectAnswer;
		}

		public string GetFalseAnswer1()
		{
			return questions[2].FalseAnswer1;
		}

		public string GetFalseAnswer2()
		{
			return questions[3].FalseAnswer2;
		}

		public string GetFalseAnswer3()
		{
			return questions[4].FalseAnswer3;
		}

	}
}
